package com.lanjiu.lib.utils.file;

import android.os.Environment;
import android.text.TextUtils;

import com.lanjiu.lib.app.App;

import java.io.File;

/**
 * @description: APP 缓存目录
 * @author: Panwei
 * @date: 2019/11/1 11:33
 */
@Deprecated
public class CacheDirectory {

    // 根目录名称
    private static final String APP_CACHE_DIRECTORY = "com.ljkj.cordial.chat";

    // 游客用户目录
    private static final String APP_CASUAL_USER_DIRECTORY = "Casuals";

    // 一级目录位用户ID

    // 二级目录
    private static final String AVATAR = "Avatar";
    private static final String MSG = "Msg";

    // 三级目录
    private static final String THUMBS = "thumb";
    private static final String IMAGES = "image";
    private static final String AUDIOS = "audio";
    private static final String VIDEOS = "video";
    private static final String FILES = "file";

    // 文件夹根目录
    private static String mAppCacheDirectory;

    private CacheDirectory() {
    }

    /**
     * 初始化文件目录
     */
    public static void init() {
        String rootPath;
        if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
                || !Environment.isExternalStorageRemovable()) {
            rootPath = App.getContext().getExternalCacheDir().getPath();
        } else {
            rootPath = App.getContext().getCacheDir().getPath();
        }
        if (!rootPath.endsWith("/") && !rootPath.endsWith(File.separator))
            rootPath += File.separator;

        mAppCacheDirectory = rootPath + APP_CACHE_DIRECTORY + File.separator;
        FileOperator.mkDirectory(mAppCacheDirectory);
    }

    /**
     * 获取文件根目录地址
     *
     * @return APP文件根目录
     */
    public static String getAppCacheDirectory() {
        if (TextUtils.isEmpty(mAppCacheDirectory)) {
            init();
        }
        return mAppCacheDirectory;
    }

    /**
     * 获取用户文件目录地址
     *
     * @param user 用户ID
     * @return 用户文件目录
     */
    public static String getUserDirectory(String user) {
        String userDirectory = getAppCacheDirectory() + user + File.separator;
        FileOperator.mkDirectory(userDirectory);
        return userDirectory;
    }

    /**
     * 获取游客用户文件目录地址
     *
     * @return 游客用户文件目录
     */
    public static String getCasualUserDirectory() {
        String casualUserDirectory = getAppCacheDirectory() + APP_CASUAL_USER_DIRECTORY + File.separator;
        FileOperator.mkDirectory(casualUserDirectory);
        return casualUserDirectory;
    }

    /**
     * 清空游客用户目录
     */
    public static void clearCasualUserDirectory() {
        FileOperator.deleteDirectory(getCasualUserDirectory(), false);
    }

    /**
     * 获取用户文件目录地址
     *
     * @param user     用户ID
     * @param isCasual 是否为游客
     * @return 用户文件目录
     */
    private static String getUserDirectory(String user, boolean isCasual) {
        if (isCasual)
            return getCasualUserDirectory();
        else
            return getUserDirectory(user);
    }

    /**
     * 获取用户头像目录
     *
     * @param user     用户ID
     * @param isCasual 是否为游客
     * @return 用户头像目录
     */
    public static String getAvatarDirectory(String user, boolean isCasual) {
        String avatarDir = getUserDirectory(user, isCasual) + AVATAR + File.separator;
        FileOperator.mkDirectory(avatarDir);
        return avatarDir;
    }

    /**
     * 获取用户缩略图文件目录地址
     *
     * @param user     用户ID
     * @param isCasual 是否为游客
     * @return 缩略图文件目录
     */
    public static String getThumbDir(String user, boolean isCasual) {
        String thumbDir = getUserDirectory(user, isCasual) + MSG + File.separator + THUMBS + File.separator;
        FileOperator.mkDirectory(thumbDir);
        return thumbDir;
    }

    /**
     * 获取用户图片文件目录地址
     *
     * @param user     用户ID
     * @param isCasual 是否为游客
     * @return 图片文件目录
     */
    public static String getImageDir(String user, boolean isCasual) {
        String imageDir = getUserDirectory(user, isCasual) + MSG + File.separator + IMAGES + File.separator;
        FileOperator.mkDirectory(imageDir);
        return imageDir;
    }

    /**
     * 获取用户语音文件目录地址
     *
     * @param user     用户ID
     * @param isCasual 是否为游客
     * @return 语音文件目录
     */
    public static String getAudioDir(String user, boolean isCasual) {
        String audioDir = getUserDirectory(user, isCasual) + MSG + File.separator + AUDIOS + File.separator;
        FileOperator.mkDirectory(audioDir);
        return audioDir;
    }

    /**
     * 获取用户视频文件目录地址
     *
     * @param user     用户ID
     * @param isCasual 是否为游客
     * @return 视频文件目录
     */
    public static String getVideoDir(String user, boolean isCasual) {
        String videoDir = getUserDirectory(user, isCasual) + MSG + File.separator + VIDEOS + File.separator;
        FileOperator.mkDirectory(videoDir);
        return videoDir;
    }

    public static String getFileDir(String user, boolean isCasual) {
        String fileDir = getUserDirectory(user, isCasual) + MSG + File.separator + FILES + File.separator;
        FileOperator.mkDirectory(fileDir);
        return fileDir;
    }
}
